﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_AdminProduct : System.Web.UI.Page
{
    private int nSkip = 0;
    private int nTake = 0;
    public ProductManager proMan = new ProductManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            loadData();
    }

    public void loadData()
    {
        gvProducts.DataSource = proMan.AllProduct();
        gvProducts.DataBind();
    }

    public void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        try
        {
            gvProducts.EditIndex = e.NewEditIndex;
            //loadData();
            int id = Convert.ToInt32(gvProducts.Rows[e.NewEditIndex].Cells[1].Text);
            Response.Redirect("EditProduct.aspx?id=" + id);
        }
        catch (Exception ex)
        {
            Response.Write("Error: " + ex.Message);
        }
    }

    public void gvProducts_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvProducts.EditIndex = -1;
        //if (btnAddNew.Enabled)
        loadData();
        //else
        //gvBooks.DeleteRow(e.RowIndex);
        //btnAddNew.Enabled = true;
    }

    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddNewProduct.aspx");
    }

    protected void gvProducts_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = Convert.ToInt32(gvProducts.Rows[e.RowIndex].Cells[1].Text);
        proMan.DeleteProduct(id);
        loadData();
    }
}